package com.fj.netty.protocol.custom;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author fj
 * @date 2024/1/11 11:39
 */
public class Test {
    public static void main(String[] args) throws Exception{
        //LengthFieldBasedFrameDecoder用于解决粘包、半包问题
        //LoggingHandler用于日志调试
        //MessageCodec自定义解码器
        EmbeddedChannel embeddedChannel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(1024,12,4,0, 0),new LoggingHandler(),new MessageCodec());
        LoginMessage loginMessage = new LoginMessage("zhangsan", "123", "张三");
//      出栈，encode
//        embeddedChannel.writeOutbound(loginMessage);
        //入栈，decode
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null,loginMessage, buffer);
        embeddedChannel.writeInbound(buffer);
    }
}
